import socket
import cv2
import struct
import threading

# 视频客户端部分
def video_client(server_ip, port):
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    cap = cv2.VideoCapture('/home/wcis/vision/test.mp4')
    while True:
        ret, frame = cap.read()
        print(ret)
        if not ret:
            break
        cv2.imshow('frame', frame)
        frame = cv2.resize(frame, (320, 240))
        encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 50]
        encoded, buffer = cv2.imencode('.jpg', frame, encode_param)
        message = struct.pack("Q", len(buffer)) + buffer.tobytes()
        if cv2.waitKey(1) & 0xff == ord('q'):
            break
            # 尝试发送数据，并捕获可能的异常以判断连接状态
        try:
            client_socket.sendto(message, (server_ip, port))
        except socket.error as e:
            print(f"Socket error: {e}")
              # 如果发送数据时出现错误，退出循环


if __name__ == "__main__":
    video_client('192.168.43.198',1234)



